package com.xspace.android.xafcommon.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.io.InputStream;

/**
 * Http请求的工具类
 *
 * @author Wubj
 */
public class HttpUtils {

    /**
     * 下载文件，返回流对象
     *
     * @param url 路径
     * @return Http返回结果的封装对象
     */
    public static HttpResult download(String url) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        boolean retry = true;//重试
        while (retry) {
            try {
                HttpResponse httpResponse = httpClient.execute(httpGet);
                if (httpResponse != null) {
                    return new HttpResult(httpClient, httpGet, httpResponse);
                }
            } catch (Exception e) {
                retry = false;
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * Http返回结果的进一步封装
     *
     * @author Administrator
     */
    public static class HttpResult {
        private HttpClient httpClient;
        private HttpGet httpGet;
        private HttpResponse httpResponse;
        private InputStream inputStream;

        public HttpResult(HttpClient httpClient, HttpGet httpGet,
                          HttpResponse httpResponse) {
            super();
            this.httpClient = httpClient;
            this.httpGet = httpGet;
            this.httpResponse = httpResponse;
        }

        /**
         * 获取状态码
         *
         * @return
         */
        public int getStatusCode() {
            StatusLine status = httpResponse.getStatusLine();
            return status.getStatusCode();
        }

        /**
         * 获取输入流
         *
         * @return
         */
        public InputStream getInputStream() {
            if (inputStream == null && getStatusCode() < 300) {
                HttpEntity entity = httpResponse.getEntity();
                try {
                    inputStream = entity.getContent();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return inputStream;
        }

        /**
         * 关闭链接和流对象
         */
        public void close() {
            if (httpGet != null) {
                httpGet.abort();
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //关闭链接
            if (httpClient != null) {
                httpClient.getConnectionManager().closeExpiredConnections();
            }
        }
    }
}

